package videoBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Objects;

/**
 * Created by yangxianming on 2020/8/14.
 * 对video管理
 */
public class VideoFileManager implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        //初始化返回对象，读取请求数据
        ReturnJson returnJson = new ReturnJson();
        JSONObject apiJson = GetRequestInputStream.getJson(request);
        String action = apiJson.getString("action");

        //获取系统名字和文件路径
        String splitMark = myConfig.getSplitMark("splitMark");
        String resource_1 = myConfig.getPropertiesConfig().getProperties().getProperty("resource_1");
        System.out.println(resource_1);

        //postgraduate资源
        if (Objects.equals(action, "getDir")){
            String myPath = apiJson.getString("path").replace("|", splitMark);
            String root = resource_1 + splitMark + "postgraduate\\" + myPath;
            System.out.println("root --- " + root);
            JSONObject dirAndFile = new JSONObject();
            JSONArray dir = new JSONArray();
            JSONArray fileArr = new JSONArray();
            File f = new File(root);
            File[] files = f.listFiles();
            for (File file: files){
                if (file.isDirectory()){
                    dir.add(file.getName());
                }else {
                    fileArr.add(file.getName());
                }
            }
            dirAndFile.put("dir", dir);
            dirAndFile.put("file", fileArr);
            System.out.println(dirAndFile);
            returnJson.setResult(dirAndFile);
            return returnJson.toString();
        }
        return returnJson.toString();
    }
}
